package com.zbkj.crmeb.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.utils.CrmebUtil;
import com.zbkj.crmeb.user.dao.UserTagDao;
import com.zbkj.crmeb.user.model.User;
import com.zbkj.crmeb.user.model.UserTag;
import com.zbkj.crmeb.user.request.UserTagRequest;
import com.zbkj.crmeb.user.service.UserService;
import com.zbkj.crmeb.user.service.UserTagService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Mr.Zhang
* @Description UserTagServiceImpl 接口实现
* @since 2020-06-05
*/
@Service
public class UserTagServiceImpl extends ServiceImpl<UserTagDao, UserTag> implements UserTagService {

    @Resource
    private UserTagDao dao;

    @Autowired
    UserService userService;


    /**
    * 列表
    * @param pageParamRequest 分页类参数
    * @author Mr.Zhang
    * @since 2020-06-05
    * @return List<UserTag>
    */
    @Override
    public List<UserTag> getList(PageParamRequest pageParamRequest) {
        return dao.selectList(null);
    }


    /**
     * 检测是否有标签已经废弃
     * @param tagIdValue String 标签id
     * @author Mr.Zhang
     * @since 2020-06-05
     * @return List<UserTag>
     */
    @Override
    public String clean(String tagIdValue) {
        LambdaQueryWrapper<UserTag> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(UserTag::getId, CrmebUtil.stringToArray(tagIdValue));
        List<UserTag> userTags = dao.selectList(lambdaQueryWrapper);
        if(null == userTags){
            return null;
        }

        return userTags.stream().map(s -> s.getId().toString()).distinct().collect(Collectors.joining(","));
    }

    /**
     * 根据id in 返回name字符串
     * @param tagIdValue String 标签id
     * @author Mr.Zhang
     * @since 2020-06-05
     * @return List<UserTag>
     */
    @Override
    public String getGroupNameInId(String tagIdValue) {
        LambdaQueryWrapper<UserTag> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(UserTag::getId, CrmebUtil.stringToArray(tagIdValue)).orderByDesc(UserTag::getId);
        List<UserTag> userTags = dao.selectList(lambdaQueryWrapper);
        if(null == userTags){
            return "无";
        }

        return userTags.stream().map(UserTag::getName).distinct().collect(Collectors.joining(","));
    }

    @Override
    public Object listTag(Integer merId) {
        return list(Wrappers.lambdaQuery(UserTag.class).eq(UserTag::getMerId,merId));
    }

    @Override
    public Object pageTag(UserTagRequest request) {
        Page<Object> page = PageHelper.startPage(request.getPageNow(), request.getPageSize());
        List<UserTag> userTagList = list(Wrappers.lambdaQuery(UserTag.class)
                .like(StringUtil.isNotEmpty(request.getKeyword()), UserTag:: getName, request.getKeyword()).eq(UserTag::getMerId,request.getMerId()));
        return CommonPage.copyPageInfo(page, userTagList);
    }

    @Override
    public boolean saveTag(UserTag request) {
        if(StringUtils.isBlank(request.getName())){
            throw new CrmebException("标签名称不能为空");
        }
        if(request.getName().length()>10){
            throw new CrmebException("标签名称长度不能大于8个字符");
        }
        UserTag userTag = getOne(Wrappers.lambdaQuery(UserTag.class).eq(UserTag::getMerId,request.getMerId()).eq(UserTag::getName, request.getName()));
        if(null != userTag){
            throw new CrmebException("该标签名称已存在");
        }
        request.setCreateTime(new Date()).setUpdateTime(new Date());
        return save(request);
    }

    @Override
    public boolean updateTag(UserTag request) {
        if(StringUtils.isBlank(request.getName())){
            throw new CrmebException("标签名称不能为空");
        }
        if(request.getName().length()>10){
            throw new CrmebException("标签名称长度不能大于8个字符");
        }
        UserTag userTag = getOne(Wrappers.lambdaQuery(UserTag.class).eq(UserTag::getId, request.getId()).eq(UserTag::getMerId, request.getMerId()));
        if(null == userTag){
            throw new CrmebException("该标签不存在");
        }
        int count = count(Wrappers.lambdaQuery(UserTag.class).eq(UserTag::getMerId,request.getMerId()).eq(UserTag::getName, request.getName()).ne(UserTag::getId, request.getId()));
        if(count > 0){
            throw new CrmebException("该标签名称已存在");
        }
        userTag.setName(request.getName());
        return updateById(userTag);
    }

    @Override
    public boolean deleteTag(Integer id, Integer merId) {
        UserTag userTag = getOne(Wrappers.lambdaQuery(UserTag.class).eq(UserTag::getId, id).eq(UserTag::getMerId, merId));
        if(null == userTag){
            throw new CrmebException("该标签不存在");
        }
        //查询有没有用户绑定该标签
        List<User> userList = userService.list(Wrappers.lambdaQuery(User.class).eq(User::getMerId,merId).apply(CrmebUtil.getFindInSetSql("tag_ids", id)));
        if(CollectionUtils.isNotEmpty(userList)){
            throw new CrmebException("该标签有用户绑定，不能删除");
        }
        return removeById(id);
    }

}

